package org.nearby.note;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class EditJFrame extends JFrame implements ActionListener, ItemListener, MouseListener {

    private JTextField textSize;
    private JCheckBox boxBold, boxItalic;
    private JButton buttonCut, buttonCopy, buttonPaste;
    private JTextArea textArea;
    private JPopupMenu popupMenu;
    private JDialog dialog;
    private JLabel labelDialog;
    private JRadioButtonMenuItem red, blue, green;
    private JCheckBoxMenuItem CboxBold, CboxItalic;

    public EditJFrame() {
        super("文本编辑器");
        this.setSize(500, 300);
        this.setLocation(300, 240);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        textArea = new JTextArea("北京北京我爱你！");
        textArea.addMouseListener(this);
        this.add(textArea);

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.add(panel, "North");
        textSize = new JTextField("12", 10);
        panel.add(textSize);
        textSize.addActionListener(this);

        boxBold = new JCheckBox("粗体");
        panel.add(boxBold);
        boxBold.addItemListener(this);

        boxItalic = new JCheckBox("斜体");
        panel.add(boxItalic);
        boxItalic.addItemListener(this);

        this.addmyMenu();
        this.setVisible(true);
    }

    private void addmyMenu() {
        // TODO Auto-generated method stub
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        JMenu menuFile = new JMenu("文件");
        menuBar.add(menuFile);
        menuFile.add(new JMenuItem("打开"));
        menuFile.add(new JMenuItem("保存"));
        menuFile.addSeparator();

        JMenuItem menuExit = new JMenuItem("退出");
        menuFile.add(menuExit);
        menuExit.addActionListener(this);

        JMenu menuEdit = new JMenu("编辑");
        menuBar.add(menuEdit);

        JMenu menuStyle = new JMenu("字形");
        CboxBold = new JCheckBoxMenuItem("粗体");
        CboxItalic = new JCheckBoxMenuItem("斜体");
        menuStyle.add(CboxBold);
        menuStyle.add(CboxItalic);
        CboxBold.addItemListener(this);
        CboxItalic.addItemListener(this);
        menuEdit.add(menuStyle);

        JMenu menuColor = new JMenu("颜色");
        menuEdit.add(menuColor);

        ButtonGroup buttonGroup = new ButtonGroup();
        red = new JRadioButtonMenuItem("红", false);
        buttonGroup.add(red);
        menuColor.add(red);
        red.addItemListener(this);

        green = new JRadioButtonMenuItem("绿", false);
        buttonGroup.add(green);
        menuColor.add(green);
        green.addItemListener(this);

        blue = new JRadioButtonMenuItem("蓝", false);
        buttonGroup.add(blue);
        menuColor.add(blue);
        blue.addItemListener(this);

        menuBar.add(new JMenu("帮助"));

        popupMenu = new JPopupMenu();

        JMenuItem itemCut = new JMenuItem("剪切");
        itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));
        popupMenu.add(itemCut);
        itemCut.addActionListener(this);

        JMenuItem itemCopy = new JMenuItem("复制");
        itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK));
        popupMenu.add(itemCopy);
        itemCopy.addActionListener(this);

        JMenuItem itemPaste = new JMenuItem("粘贴");
        itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_DOWN_MASK));
        popupMenu.add(itemPaste);
        itemPaste.addActionListener(this);

        textArea.add(popupMenu);

        dialog = new JDialog(this, "提示");
        dialog.setSize(240, 100);
        labelDialog = new JLabel("", JLabel.CENTER);
        dialog.add(labelDialog);
        dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getActionCommand() == "退出") {
            System.exit(0);
        }
        if (e.getActionCommand() == "剪切") {
            textArea.cut();
        }
        if (e.getActionCommand() == "复制") {
            textArea.copy();
        }
        if (e.getActionCommand() == "粘贴") {
            textArea.paste();
        }
        if (e.getSource() == textSize) {
            int size = 0;
            try {
                size = Integer.parseInt(textSize.getText());
                if (size <= 0 || size > 72)
                    throw new Exception("SizeException");
                java.awt.Font font = textArea.getFont();
                textArea.setFont(new Font(font.getName(), font.getStyle(), size));
            } catch (NumberFormatException nfe) {
                labelDialog.setText("\"" + textSize.getText() + "\"不能转换成整数，请重新输入！");
                dialog.setLocation(this.getX() + 100, this.getY() + 100);
                dialog.setVisible(true);
            } catch (Exception ex) {
                if (ex.getMessage() == "SizeException") {
                    labelDialog.setText(size + " 字号不合适，请重新输入！");
                    dialog.setLocation(this.getX() + 100, this.getY() + 100);
                    dialog.setVisible(true);
                }
            } finally {
            }
        }
    }

    public void itemStateChanged(ItemEvent e) {
        Font font = textArea.getFont();
        int style = font.getStyle();
        if (e.getSource() == boxBold || e.getSource() == CboxBold) {
            style ^= 1;
        }
        if (e.getSource() == boxItalic || e.getSource() == CboxItalic) {
            style ^= 2;
        }
        if (e.getSource() == red) {
            textArea.setForeground(Color.red);
        }
        if (e.getSource() == green) {
            textArea.setForeground(Color.green);
        }
        if (e.getSource() == blue) {
            textArea.setForeground(Color.blue);
        }
        textArea.setFont(new Font(font.getName(), style, font.getSize()));
    }

    // 也可以通过适配器实现
    public void mouseClicked(MouseEvent mec) {
        if (mec.getModifiers() == mec.BUTTON3_MASK)
            popupMenu.show(textArea, mec.getX(), mec.getY());
    }

    public void mouseEntered(MouseEvent arg0) {
    }

    public void mouseExited(MouseEvent arg0) {
    }

    public void mousePressed(MouseEvent arg0) {
    }

    public void mouseReleased(MouseEvent arg0) {
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new EditJFrame();
    }
}